﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.OA;
using SmartSpace.Web;

namespace SmartSpace.WebApp.Modules.OA.Pages
{
    public partial class Asset_Add : SsTemplatePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SsCtlUtil.Picker(this, "org", _ctlOrgPicker, txt_orgId, _ctlHiddenOrgId);
            SsCtlUtil.Picker(this, "asset", _ctlTypePicker, _ctlEditType, _ctlHiddenTypeCode);

            string orgId = Request.QueryString["orgId"] ?? string.Empty;
            if (!IsPostBack)
            {
                this.txt_orgId.Text = orgId;
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            string orgId = Request.QueryString["orgId"] ?? string.Empty;
            OAService service = new OAService();

            Asset asset = new Asset();
            asset.Id = Guid.NewGuid();
            asset.Code = this.txt_Code.Text;
            asset.OrgId = new Guid(orgId);
            asset.CreatedDate = DateTime.Now;
            asset.Creator = this.User.Identity.Name;
            asset.Caption = this.txt_Caption.Text;
            asset.TypeCode = _ctlHiddenTypeCode.Value;
            asset.TypeCaption = _ctlEditType.Text;

            ServiceResult result = service.SaveAsset(asset);
            if (result.IsSuccess)
            {
                Response.Redirect("Asset-List.aspx?orgId=" + orgId);
            }
        }


        protected void btn_Cannel_Click(object sender, EventArgs e)
        {
            string orgId = Request.QueryString["orgId"] ?? string.Empty;
            Response.Redirect("Asset-List.aspx?orgId=" + orgId);
        }
    }
}